/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseManeger;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import me.aviator.entity.HibernateUtil;
import me.aviator.service.impl.SymbolService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * FXML Controller class
 *
 * @author Administrator
 */
public class SymbolFormController implements Initializable {
    @FXML
    private AnchorPane RootPane;
    @FXML
    private Button BtnCancel;
    @FXML
    private Button BtnEnsure;
    @FXML
    private TextField FileName;
    @FXML
    private TextField FilePath;
    @FXML
    private Button BtnAddFile;
    private File file=null;
    private String PathString=null;
    private String NameString=null;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void HandleBtnCancel(ActionEvent event) {
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnEnsure(ActionEvent event) {
        String type="";
        String catagoryA="";
        String catagoryB="";
        int orderID=0;
        String tempStr=NameString.substring(0, 2);
        switch(tempStr)
        {
            case "AS":
                type="艺术符号";
                break;
            case "GS":
                type="几何符号";
                break;
        }
        tempStr=NameString.substring(2, 4);
        String tempStr1=NameString.substring(4,6);
        if(type.equals("艺术符号")){
        switch(tempStr)
        {
            case "NC":
                catagoryA="自然条件";
                switch(tempStr1)
                {
                    case "GE":
                        catagoryB="地质";
                        break;
                    case "LA":
                        catagoryB="地貌";
                        break;
                    case "CL":
                        catagoryB="气候";
                        break;
                    case "HY":
                        catagoryB="水文";
                        break;
                    case "SO":
                        catagoryB="土壤";
                        break;
                    case "VE":
                        catagoryB="植被";
                        break;
                    case "AN":
                        catagoryB="动物";
                        break;
                    case "NP":
                        catagoryB="自然保护区";
                        break;
                }
                break;
            case "NR":
                catagoryA="自然资源";
                switch(tempStr1)
                {
                    case "LR":
                        catagoryB="土地资源";
                        break;
                    case "WR":
                        catagoryB="水资源";
                        break;
                    case "MR":
                        catagoryB="矿产资源";
                        break;
                    case "TR":
                        catagoryB="森林资源";
                        break;
                    case "OR":
                        catagoryB="海洋资源";
                        break;
                    case "TO":
                        catagoryB="旅游资源";
                        break;
                }
                break;
            case "BI":
                catagoryA="基础设施";
                switch(tempStr1)
                {
                    case "TP":
                        catagoryB="交通运输";
                        break;
                    case "PT":
                        catagoryB="邮电通信";
                        break;
                }
                break;
            case "CE":
                catagoryA="社会经济";
                switch(tempStr1)
                {
                    case "PO":
                        catagoryB="人口";
                        break;
                    case "EN":
                        catagoryB="综合经济";
                        break;
                    case "ED":
                        catagoryB="文化教育";
                        break;
                    case "HC":
                        catagoryB="医疗卫生";
                        break;
                }
                break;
            case "EC":
                catagoryA="生态环境";
                switch(tempStr1)
                {
                    case "ND":
                        catagoryB="自然灾害";
                        break;
                    case "EE":
                        catagoryB="生态环境变化";
                        break;
                    case "EP":
                        catagoryB="环境污染";
                        break;
                    
                }
                break;
            default:
                break;
        }
        }
        else{
            switch(tempStr){
                case "PF":
                    catagoryA="平面符号";
                    break;    
                case "SF":
                    catagoryA="立体符号";
                    break;
            }
            switch(tempStr1){
                case "CI":
                    catagoryB="圆形";
                    break;
                case "TR":
                    catagoryB="三角形";
                    break;
                case "RE":
                    catagoryB="矩形";
                    break;
                case "PO":
                    catagoryB="多边形";
                    break;
                case "ST":
                    catagoryB="星形";
                    break;
            }
        }
        tempStr=NameString.substring(6, 8);
        orderID=Integer.parseInt(tempStr);
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        Session sess=sessionFactory.openSession();
        Transaction tx=sess.beginTransaction();
        
        SymbolService symbolService=new SymbolService(sess);
        symbolService.uploadSymbol(NameString, type, file, catagoryA, catagoryB, orderID);
        
        tx.commit();
//        sess.close();
//        sessionFactory.close();
        
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnAddFile(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("导入SVG符号文件");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); 
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("SVG文件", "*.svg;*.SVG")
        );
        Stage s = new Stage(); 
        
        file= fileChooser.showOpenDialog(s);
        if (file != null) {
            PathString=file.getPath();
            FilePath.setText(PathString);
            NameString=file.getName();
            FileName.setText(NameString);
        }
    }
    
}
